package com.emmmya.ocs.modules.system.file.controller;


import com.emmmya.ocs.modules.system.annotation.OperateLog;
import com.emmmya.ocs.common.utils.ResultUtil;
import com.emmmya.ocs.common.vo.Result;
import com.emmmya.ocs.config.redis.SystemCacheRedisManager;
import com.emmmya.ocs.modules.system.file.entity.Directory;
import com.emmmya.ocs.modules.system.file.entity.File;
import com.emmmya.ocs.modules.system.file.entity.UIDirectory;
import com.emmmya.ocs.modules.system.file.service.DirectoryService;
import com.emmmya.ocs.modules.system.file.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("directory")
// "文件夹管理器控制层"
public class DirectoryController {
    @Autowired
    private DirectoryService directoryService;
    @Autowired
    private FileService fileService;

    @Autowired
    private SystemCacheRedisManager sysCacheRedisTemplate;

    @GetMapping(value="/clearCache")
    @OperateLog(value="清楚缓存",operateType = 2,module ="文件夹模块")
    public Result<Object> clearCache(@RequestParam("dirId") Long dirId){
        return directoryService.clearCache(dirId);
    }

    @RequestMapping(value="/createDir",method = RequestMethod.POST)
    @CacheEvict(cacheNames = "directory",key="#dir.parentId")
    @OperateLog(value="创建文件夹",operateType = 2,module = "文件夹模块")
    public Result<Object> createDir(@RequestBody Directory dir){
        //System.out.println(dir);
        Directory save = null;
        try {
            save = directoryService.createDir(dir);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(e.getMessage());
        }
        return ResultUtil.data(save,"保存成功！");
    }
    @RequestMapping(value = "/getDir",method = RequestMethod.POST)
    @Cacheable(cacheNames = "directory",key="#dirId")
    @OperateLog(value="根据id查询文件夹",operateType = 1,module = "文件夹模块")
    public Result<Object> getDir(@RequestParam("dirId") Long dirId){
        //System.out.println(dirId);
        Directory dir = directoryService.get(dirId);
        //System.out.println(dir);
        List<Directory> cliDirs = directoryService.findByParentId(dirId);
        //查询当前文件夹的文件内容
        List<File> cliFiles = fileService.findByDirId(dirId);
        UIDirectory uiDir = new UIDirectory();
        uiDir.setDir(dir);
        uiDir.setCliDirs(cliDirs);
        uiDir.setCliFiles(cliFiles);
        return ResultUtil.data(uiDir,"查询成功");
    }
    @RequestMapping(value = "/deleteDir",method = RequestMethod.POST)
    @CacheEvict(cacheNames = "directory",key="#dirId")
    @OperateLog(value="删除文件夹",operateType = 4,module = "文件夹模块")
    public Result<Object> deleteDir(@RequestParam("dirId") Long dirId){
        try {
            Directory dir = directoryService.get(dirId);
            if(dir!=null){
                directoryService.delete(dir.getId());
                sysCacheRedisTemplate.del("directory::"+dir.getParentId());
                return ResultUtil.success("删除成功");
            }else{
                return ResultUtil.error("文件夹已不存在，请刷新页面");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(e.getMessage());
        }
    }
    @RequestMapping(value = "/reName",method = RequestMethod.POST)
    @CachePut(cacheNames = "directory",key="#dirId")
    @OperateLog(value="重命名文件夹",operateType = 3,module = "文件夹模块")
    public Result<Object> reName(@RequestParam("id") Long dirId,@RequestParam("newName")String newName ){
        System.out.println("dirId:"+dirId+",newName:"+newName);
        directoryService.reName(dirId,newName);
        return ResultUtil.success("2333");
    }
}
